package ligne2vie.topos.client.panels.search;

import ligne2vie.topos.client.asynccalls.GetAndDisplaySummits;
import ligne2vie.topos.client.maps.MyMapWidget;
import ligne2vie.topos.client.service.SummitService;
import ligne2vie.topos.client.service.SummitServiceAsync;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.maps.client.base.HasLatLng;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.widgetideas.client.SliderBar;

/**
 * The class {@link SearchSummitsNearPosition} provides a whole panel authorizing georequesting summits near the map
 * central position.
 * 
 * @author thuguerre
 * @since 1.1.0
 */
public class SearchSummitsNearPosition extends VerticalPanel {

	private final SummitServiceAsync summitService = GWT.create(SummitService.class);

	private Label searchSummitLabel = new Label("Rechercher tous les sommets distants de :");
	private SearchSummitSlider searchSummitSlider = new SearchSummitSlider();
	private SearchSummitOkButton searchSummitOkButton = new SearchSummitOkButton();

	/**
	 * Default constructor.
	 */
	public SearchSummitsNearPosition() {

		this.setWidth("100%");
		
		this.add(searchSummitLabel);
		this.add(searchSummitSlider);
		this.setHorizontalAlignment(ALIGN_RIGHT);
		this.add(searchSummitOkButton);
	}

	/**
	 * The class {@link SearchSummitSlider} provides a convenience base to display the slider used to search summits
	 * near a position.
	 * 
	 * @author thuguerre
	 * @since 1.1.0
	 * 
	 * @see <pre>
	 * http://www.google.com/codesearch/p?hl=fr#kxqvj77bOTo/trunk/src/com/google/gwt/demos/sliderbar/client/SliderBarDemo.java&q=sliderbar%20package:http://google-web-toolkit-incubator%5C.googlecode%5C.com&sa=N&cd=9&ct=rc
	 * </pre>
	 */
	private class SearchSummitSlider extends SliderBar {

		/**
		 * Default constructor.
		 */
		public SearchSummitSlider() {

			super(0.0, 200.0);

			this.setCurrentValue(30.0);
			this.setNumLabels(4);
			this.setNumTicks(9);
			this.setStepSize(5.0);
			this.setSize("100%", "50px");
			this.setLabelFormatter(new LabelFormatter() {

				@Override
				public String formatLabel(SliderBar slider, double value) {
					return value + " km";
				}
			});
		}
	}

	/**
	 * The class {@link SearchSummitOkButton} provides the button launching the georequests on summits near a position.
	 * 
	 * @author thuguerre
	 * @since 1.1.0
	 */
	private class SearchSummitOkButton extends Button implements ClickHandler {

		/**
		 * Default constructor.
		 */
		public SearchSummitOkButton() {

			super("OK");
			this.addClickHandler(this);
		}

		/**
		 * {@inheritDoc}
		 */
		@Override
		public void onClick(ClickEvent event) {

			HasLatLng mapCenter = MyMapWidget.getInstance().getCenter();

			summitService.findSummitsAround(mapCenter.getLatitude(), mapCenter.getLongitude(),
					searchSummitSlider.getCurrentValue(), new GetAndDisplaySummits());
		}
	}
}
